/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.objects.extra;

import org.easyway.annotations.Optimized;
import org.easyway.interfaces.base.ITexture;
import org.easyway.interfaces.extended.IDrawing;
import org.easyway.interfaces.extended.ILayerID;
import org.easyway.objects.BaseObject;
import org.easyway.objects.texture.TextureCompact;
import org.easyway.system.StaticRef;
import org.easyway.system.state.OpenGLState;
import org.lwjgl.opengl.GL11;

@Optimized
public class StarsManager extends BaseObject implements IDrawing, ILayerID {

	static final long serialVersionUID = 28;

	ITexture image;

	private boolean imageIsLocal;

	Stella smallStars[];

	Stella bigStars[];

	int layer;

	/**
	 * the drawing sheet
	 */
	private int idLayer = -1;

	public StarsManager(String path, int num) {
		this.image = new TextureCompact(path);
		imageIsLocal = true;
		smallStars = new Stella[num / 2];
		bigStars = new Stella[num / 2];
		for (int i = 0; i < num / 2; i++) {
			smallStars[i] = new Stella(true);
			bigStars[i] = new Stella(false);
		}
	}

	public StarsManager(ITexture image, int num) {
		this.image = image;
		imageIsLocal = false;
		smallStars = new Stella[num / 2];
		bigStars = new Stella[num / 2];
		for (int i = 0; i < num / 2; i++) {
			smallStars[i] = new Stella(true);
			bigStars[i] = new Stella(false);
		}
	}

	public void render() {
		OpenGLState.enableBlending();
		image.bind();
		GL11.glColor4f(1, 1, 1, 0.25f);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_DST_ALPHA);

		GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
		for (int i = 0; i < smallStars.length; i++)
			smallStars[i].render();
		GL11.glColor4f(1, 1, 1, 0.5f);
		for (int i = 0; i < bigStars.length; i++)
			bigStars[i].render();
		GL11.glEnd();

		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		// GL11.glDisable(GL11.GL_BLEND);
	}

	public void setLayer(int layer) {
		this.layer = layer;
		this.readdToDrawingLists();
	}

	public int getLayer() {
		return layer;
	}

	public void onDestroy() {
		if (imageIsLocal)
			image.destroy();
	}

	class Stella {

		float x, y;

		float r, g, b;

		int size;

		float speed;

		public Stella(boolean small) {
			x = (float) Math.random() * StaticRef.getCamera().getWidth();
			y = (float) Math.random() * StaticRef.getCamera().getHeight();
			r = (float) Math.random() / 2;
			g = (float) Math.random() / 2;
			b = (float) Math.random() / 2;
			double rnd = Math.random();
			if (small) {
				size = (int) (8 + rnd * 4);
				speed = (int) (1 + rnd * 2);
			} else {
				size = (int) (16 + rnd * 4);
				speed = (int) (2 + rnd * 2);
			}
		}

		public void render() {
			x -= speed;
			if (x + size <= 0) {
				x = StaticRef.getCamera().getWidth();
				y = (float) Math.random() * StaticRef.getCamera().getHeight();
			}

			// colore
			//
			// disegno
			GL11.glColor3f(r, g, b);
			// {
			// GL11.glBegin(GL11.GL_QUADS); // Draw A Quad
			GL11.glTexCoord2f(image.getXStart(), image.getYStart());
			GL11.glVertex2f(x, y);
			GL11.glTexCoord2f(image.getXEnd(), image.getYStart());
			GL11.glVertex2f(x + size, y);
			GL11.glTexCoord2f(image.getXStart(), 1);
			GL11.glVertex2f(x, y + size);
			GL11.glTexCoord2f(image.getXEnd(), image.getYEnd());
			GL11.glVertex2f(x + size, y + size);
			// }
			// GL11.glEnd();
		}

	}

	// -----------------------------------------------------------------
	// ---------------------------- IDLAYER-----------------------------
	// -----------------------------------------------------------------

	public int getIdLayer() {
		return idLayer;
	}

	public void setIdLayer(int id) {
		if (idLayer != -1) {
			StaticRef.layers[idLayer].remove(this);
		}
		if (id < 0) {
			id = 0;
		} else if (id > StaticRef.layers.length) {
			id = StaticRef.layers.length;
		}
		idLayer = id;
		StaticRef.layers[idLayer].add(this);
	}
}
